package com.mc.read.adminapi.manager;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mc.read.common.constant.Constants;
import com.mc.read.common.emun.ServiceCodeEnum;
import com.mc.read.common.exception.ServiceException;
import com.mc.read.dao.mapper.BookinformationMapper;
import com.mc.read.dao.mapper.BooklabelMapper;
import com.mc.read.dao.mapper.LabelMapper;
import com.mc.read.dao.mapper.LabelauditMapper;
import com.mc.read.dao.models.*;
import com.mc.read.dao.models.sys.Sysuser;
import com.mc.read.dao.vo.BookPageVo;
import com.mc.read.dao.vo.PageResposeVo;
import com.mc.read.vo.request.BaseRequest;
import com.mc.read.vo.respose.BaseRespose;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.Date;
import java.util.List;

/**
 * @author liuhao
 * @date 2019/6/14 17:19
 */
@Component
@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = 36000, rollbackFor = Exception.class)
public class LabelManager {
    @Autowired
    CacheManager cacheManager;
    @Autowired
    LabelauditMapper labelauditMapper;
    @Autowired
    BookinformationMapper bookinformationMapper;
    @Autowired
    BooklabelMapper booklabelMapper;
    @Autowired
    LabelMapper labelMapper;

    /**
     * @Author:LiuHao
     * @Date:15:24 2019/5/31
     * 查询标签审核列表 selectLabelAuditList
     **/
    public BaseRespose selectLabelAuditList(BaseRequest baseRequest) {
        BaseRespose baseRespose = new BaseRespose();
        BookPageVo bookPageVo = null;
        try {
            bookPageVo = JSON.parseObject(JSON.toJSONString(baseRequest.getRequestString()), BookPageVo.class);
        } catch (Exception e) {
            throw new ServiceException(ServiceCodeEnum.INFO_IS_FAIL);
        }
        PageHelper.startPage(bookPageVo.getPagenum(), bookPageVo.getPagesize());
        List<LabelauditCustom> labelauditCustomList = labelauditMapper.selectLabelAuditList(bookPageVo);
        PageInfo<LabelauditCustom> pageInfo = new PageInfo<>(labelauditCustomList);
        PageResposeVo pageResposeVo = new PageResposeVo(pageInfo.getTotal(), pageInfo.getList());
        baseRespose.setReturnString(pageResposeVo);

        return baseRespose;
    }

    /**
     * @Author:LiuHao
     * @Date:15:24 2019/5/31
     * 标签审核 不通过 labelAuditNoPass
     **/
    public BaseRespose labelAuditNoPass(BaseRequest baseRequest) {
        BaseRespose baseRespose = new BaseRespose();
        Labelaudit labelaudit = null;
        try {
            labelaudit = JSON.parseObject(JSON.toJSONString(baseRequest.getRequestString()), Labelaudit.class);
        } catch (Exception e) {
            throw new ServiceException(ServiceCodeEnum.INFO_IS_FAIL);
        }
        if (ObjectUtils.isEmpty(labelaudit.getLabelauditid()) || ObjectUtils.isEmpty(labelaudit.getNopasstxt())) {
            throw new ServiceException(203020, "请求参数不完整", JSON.toJSONString(baseRequest));
        }
        Labelaudit labelaudit1=labelauditMapper.selectByPrimaryKey(labelaudit.getLabelauditid());
        if(ObjectUtils.isEmpty(labelaudit1)){
            throw new ServiceException(203020, "未找到该记录",JSON.toJSONString(baseRequest));
        }
        if(labelaudit1.getStatus()!=0){
            throw new ServiceException(203020, "只能审核未审核状态的记录",JSON.toJSONString(baseRequest));
        }
        Sysuser sysuser = cacheManager.getSysUser(baseRequest.getUserId());
        labelaudit.setAuditdate(new Date());
        labelaudit.setStatus(2);
        labelaudit.setAudituserid(sysuser.getUserid());
        labelaudit.setAuditusername(sysuser.getName());
        labelauditMapper.updateByPrimaryKeySelective(labelaudit);
        baseRespose.setReturnString(" ");
        return baseRespose;
    }
    /**
     * @Author:LiuHao
     * @Date:15:24 2019/5/31
     * 标签审核 通过 labelAuditPass
     **/
    public BaseRespose labelAuditPass(BaseRequest baseRequest) {
        BaseRespose baseRespose = new BaseRespose();

        Labelaudit labelaudit = null;
        try {
            labelaudit = JSON.parseObject(JSON.toJSONString(baseRequest.getRequestString()), Labelaudit.class);
        } catch (Exception e) {
            throw new ServiceException(ServiceCodeEnum.INFO_IS_FAIL);
        }
        if (ObjectUtils.isEmpty(labelaudit.getLabelauditid())) {
            throw new ServiceException(203020, "请求参数不完整", JSON.toJSONString(baseRequest));
        }
        Labelaudit labelaudit1=labelauditMapper.selectByPrimaryKey(labelaudit.getLabelauditid());
        if(ObjectUtils.isEmpty(labelaudit1)){
            throw new ServiceException(203020, "未找到该记录",JSON.toJSONString(baseRequest));
        }
        if(labelaudit1.getStatus()!=0){
            throw new ServiceException(203020, "只能审核未审核状态的记录",JSON.toJSONString(baseRequest));
        }
        Sysuser sysuser = cacheManager.getSysUser(baseRequest.getUserId());
        labelaudit.setAuditdate(new Date());
        labelaudit.setStatus(1);
        labelaudit.setAudituserid(sysuser.getUserid());
        labelaudit.setAuditusername(sysuser.getName());
        labelauditMapper.updateByPrimaryKeySelective(labelaudit);

        Bookinformation bookinformation=bookinformationMapper.selectByPrimaryKey(labelaudit1.getBookid());
        if(ObjectUtils.isEmpty(bookinformation)){
            throw new ServiceException(203020, "未找到该书籍",JSON.toJSONString(baseRequest));
        }
        BooklabelExample booklabelExample=new BooklabelExample();
        booklabelExample.createCriteria().andBookidEqualTo(labelaudit.getBookid());
        int count=booklabelMapper.countByExample(booklabelExample);
        if(count>= Constants.BOOK_SUM_LABEL){
            throw new ServiceException(203020, "该书籍的标签已满", JSON.toJSONString(baseRequest));
        }

        LabelExample labelExample = new LabelExample();
        labelExample.createCriteria().andBooktypeidEqualTo(bookinformation.getMainlevelid()).andLabelnameEqualTo(labelaudit1.getLabelname());
        List<Label> labelList = labelMapper.selectByExample(labelExample);
        Integer labelid=0;
        if (labelList.size() == 0) {
            Label label = new Label();
            label.setBooktypeid(bookinformation.getMainlevelid());
            label.setLabelname(labelaudit1.getLabelname());
            labelMapper.insertGetId(label);
            labelid = label.getLabelid();
        } else {
            labelid = labelList.get(0).getLabelid();
            BooklabelExample booklabelExample1=new BooklabelExample();
            booklabelExample1.createCriteria().andBookidEqualTo(bookinformation.getBookinformationid()).andLabelidEqualTo(labelid);
            List<Booklabel> booklabelList=booklabelMapper.selectByExample(booklabelExample1);
            if(booklabelList.size()>0){
                throw new ServiceException(203020, "该书籍已存在该书签",JSON.toJSONString(baseRequest));
            }
        }
        Booklabel booklabel = new Booklabel();
        booklabel.setLabelid(labelid);
        booklabel.setBookid(bookinformation.getBookinformationid());
        booklabel.setLikesum(0);
        booklabel.setHatesum(0);
        booklabelMapper.insertSelective(booklabel);
        baseRespose.setReturnString(" ");

        return baseRespose;
    }
    /**
     * @Author:LiuHao
     * @Date:15:24 2019/5/31
     * 删除标签审核记录 deleteLabelAudit
     **/
    public BaseRespose deleteLabelAudit(BaseRequest baseRequest) {
        BaseRespose baseRespose = new BaseRespose();

        Labelaudit labelaudit = null;
        try {
            labelaudit = JSON.parseObject(JSON.toJSONString(baseRequest.getRequestString()), Labelaudit.class);
        } catch (Exception e) {
            throw new ServiceException(ServiceCodeEnum.INFO_IS_FAIL);
        }
        if (ObjectUtils.isEmpty(labelaudit.getLabelauditid())) {
            throw new ServiceException(203020, "请求参数不完整", JSON.toJSONString(baseRequest));
        }
        labelauditMapper.deleteByPrimaryKey(labelaudit.getLabelauditid());

        baseRespose.setReturnString(" ");

        return baseRespose;
    }




}
